package com.beta.restapi.controller.upload;

import com.beta.entity.Users;
import com.beta.service.IUsersService;
import com.beta.support.ResourceImagePathBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lichao on 2016/12/12.
 */
@Component
public class IdCardImageHandler extends AbstractImageHandler implements IImageHandler {

    @Autowired
    private IUsersService usersService;

    @Override
    public boolean supportType(String pictureType) {
        return "id_card_picture".equals(pictureType);
    }

    @Override
    public String buildRelativePathForSave(HttpServletRequest request, Integer userId, String fileName) {
        return ResourceImagePathBuilder.userIdCardPicPath(userId, fileName);
    }

    @Override
    protected String buildImageRelativePathForDelete(HttpServletRequest request, Integer userId) {
        Users users = usersService.selectById(userId);
        return ResourceImagePathBuilder.userIdCardPicPath(userId, users.getIdCardPicture());
    }

    @Override
    public void afterImageSave(HttpServletRequest request, Integer userId, String fileName) {
        usersService.updateIdCardPicture(userId, fileName);
    }

    @Override
    protected void afterImageDelete(HttpServletRequest request, Integer userId) {
        super.afterImageDelete(request, userId);
        usersService.deleteIdCardPicture(userId);
    }
}
